package com.woniuxy;

import java.util.*;

/**
 * @author 作者
 * @create 2025-09-11 16:22
 */

public class Test03 {
    public static void main(String[] args) {

    }

    private static void test09() {
        HashSet<Integer> nums = new HashSet<>();
        Random random = new Random();
        int num;
        do {
            num = random.nextInt(20);
//            if (!nums.contains(num)) {
                nums.add(num);
//            }
        } while (nums.size() < 10);
        System.out.println(nums);
    }

    private static void test08() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("aaa");
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        System.out.println(list);

        HashSet<String> set = new HashSet<>();
        for (String str : list) {
            set.add(str);
        }
        System.out.println(set);

        ArrayList<String> strings = new ArrayList<>(set);
        System.out.println(strings);
    }

    private static void test07() {
        Set<String> strs = new HashSet<>();
        strs.add("aaa");
        strs.add("bbb");
        strs.add("ccc");
        strs.add("ddd");
        strs.add("eee");
        //添加顺序和内部排列顺序不一致(内部的排序规则)
        System.out.println(strs);
        for (String s : strs) {
            System.out.println(s);
        }
    }

    private static void test06() {
        Set<String> strs = new HashSet<>();
        strs.add("aaa");
        strs.add("bbb");
        strs.add("ccc");
        strs.add("aaa");
        strs.add("aaa");
        strs.add("aaa");
        strs.add("aaa");
        strs.add("bbb");
        strs.add("ccc");
        System.out.println(strs);
    }

    private static void test05() {
        List<String> strs = new ArrayList<>();
        strs.add("bbb");
        strs.add("aaa");
        strs.add("ccc");
        System.out.println(strs);
        Collections.sort(strs);
        System.out.println(strs);

        List<Book> books = new ArrayList<>();
        books.add(new Book(1, "xxx", 80));
        books.add(new Book(2, "bbb", 10));
        books.add(new Book(3, "yyy", 50));
        books.add(new Book(4, "aaa", 20));
        System.out.println(books);
        //自然排序
        Collections.sort(books);
        System.out.println(books);
        //自定义排序
        Collections.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                return o2.getPrice().compareTo(o1.getPrice());
            }
        });
        System.out.println(books);
        Collections.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                return o1.getBname().compareTo(o2.getBname());
            }
        });
        System.out.println(books);
    }

    private static void test04() {
        List<Book> books = new ArrayList<>();
        books.add(new Book(1, "aaa", 80));
        books.add(new Book(2, "bbb", 10));
        books.add(new Book(3, "ccc", 50));
        books.add(new Book(4, "aaa", 20));
        System.out.println(books);

        for (int i = 0; i < books.size() - 1; i++) {
            for (int j = 0; j < books.size() - 1 - i; j++) {
                if (books.get(j).getPrice() > books.get(j + 1).getPrice()) {
                    Book book = books.get(j);
                    books.set(j, books.get(j + 1));
                    books.set(j + 1, book);
                }
            }
        }

        System.out.println(books);
    }

    private static void test03() {
        List<Integer> nums = new ArrayList<>();
        Random random = new Random();
        int num;
        do {
            num = random.nextInt(20);
            if (!nums.contains(num)) {
                nums.add(num);
            }
        } while (nums.size() < 10);
        System.out.println(nums);
    }

    private static void test02() {
        List<Book> books = new ArrayList<>();
        books.add(new Book(1, "aaa", 80));
        books.add(new Book(2, "bbb", 10));
        books.add(new Book(3, "ccc", 50));
        books.add(new Book(1, "aaa", 80));

        System.out.println(books);

        for (int i = 0; i < books.size() - 1; i++) {
            for (int j = i + 1; j < books.size(); j++) {
                if (books.get(i).equals(books.get(j))) {
                    books.remove(j);
                    j--;
                }
            }
        }
        System.out.println(books);
    }

    private static void test01() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("aaa");
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        System.out.println(list);

        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).equals(list.get(j))) {
                    list.remove(j);
                    j--;
                }
            }
        }
        System.out.println(list);
    }
}
